#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 基础URL
BASE_URL="http://localhost:8000/api/v1"
TOKEN=""
REPORT_ID=""

# 测试结果统计
TOTAL_TESTS=0
PASSED_TESTS=0

# 辅助函数
print_test_result() {
    local test_name=$1
    local result=$2
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    if [ $result -eq 0 ]; then
        echo -e "${GREEN}✓ $test_name passed${NC}"
        PASSED_TESTS=$((PASSED_TESTS + 1))
    else
        echo -e "${RED}✗ $test_name failed${NC}"
    fi
}

# 获取管理员token
get_admin_token() {
    response=$(curl -s -X POST "${BASE_URL}/user/login/" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "admin",
            "password": "admin123"
        }')
    
    TOKEN=$(echo "$response" | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['token'])")
}

# 1. 测试创建报告
test_create_report() {
    echo -e "\n${YELLOW}Testing Create Report...${NC}"
    
    response=$(curl -s -X POST "${BASE_URL}/industry-report/" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "title": "测试产业报告",
            "industry": "IT",
            "content": "这是一份测试报告的内容",
            "summary": "测试报告摘要",
            "tags": ["测试", "IT"],
            "status": "draft"
        }')
    
    echo "Response: $response"
    if echo "$response" | grep -q '"code": 200'; then
        REPORT_ID=$(echo "$response" | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['report_id'])")
        print_test_result "Create Report" 0
    else
        print_test_result "Create Report" 1
    fi
}

# 2. 测试获取报告列表
test_get_report_list() {
    echo -e "\n${YELLOW}Testing Get Report List...${NC}"
    
    response=$(curl -s -X GET "${BASE_URL}/industry-report/list/?page=1&pageSize=20" \
        -H "Authorization: Bearer $TOKEN")
    
    echo "Response: $response"
    if echo "$response" | grep -q '"code": 200'; then
        print_test_result "Get Report List" 0
    else
        print_test_result "Get Report List" 1
    fi
}

# 3. 测试获取报告详情
test_get_report_detail() {
    echo -e "\n${YELLOW}Testing Get Report Detail...${NC}"
    
    response=$(curl -s -X GET "${BASE_URL}/industry-report/${REPORT_ID}/" \
        -H "Authorization: Bearer $TOKEN")
    
    echo "Response: $response"
    if echo "$response" | grep -q '"code": 200'; then
        print_test_result "Get Report Detail" 0
    else
        print_test_result "Get Report Detail" 1
    fi
}

# 4. 测试更新报告
test_update_report() {
    echo -e "\n${YELLOW}Testing Update Report...${NC}"
    
    response=$(curl -s -X PUT "${BASE_URL}/industry-report/${REPORT_ID}/" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "title": "更新后的测试报告",
            "summary": "更新后的摘要"
        }')
    
    echo "Response: $response"
    if echo "$response" | grep -q '"code": 200'; then
        print_test_result "Update Report" 0
    else
        print_test_result "Update Report" 1
    fi
}

# 5. 测试删除报告
test_delete_report() {
    echo -e "\n${YELLOW}Testing Delete Report...${NC}"
    
    response=$(curl -s -X DELETE "${BASE_URL}/industry-report/${REPORT_ID}/" \
        -H "Authorization: Bearer $TOKEN")
    
    echo "Response: $response"
    if echo "$response" | grep -q '"code": 200'; then
        print_test_result "Delete Report" 0
    else
        print_test_result "Delete Report" 1
    fi
}

# 6. 测试未授权访问
test_unauthorized_access() {
    echo -e "\n${YELLOW}Testing Unauthorized Access...${NC}"
    
    response=$(curl -s -X GET "${BASE_URL}/industry-report/list/")
    
    echo "Response: $response"
    if echo "$response" | grep -q '"code": 401'; then
        print_test_result "Unauthorized Access" 0
    else
        print_test_result "Unauthorized Access" 1
    fi
}

# 运行所有测试
run_all_tests() {
    echo -e "${YELLOW}Starting Industry Report API Tests...${NC}"
    
    # 获取管理员token
    get_admin_token
    
    # 运行测试
    test_create_report
    test_get_report_list
    test_get_report_detail
    test_update_report
    test_delete_report
    test_unauthorized_access
    
    # 打印测试结果统计
    echo -e "\n${YELLOW}Test Results:${NC}"
    echo -e "Total Tests: $TOTAL_TESTS"
    echo -e "Passed: ${GREEN}$PASSED_TESTS${NC}"
    echo -e "Failed: ${RED}$((TOTAL_TESTS - PASSED_TESTS))${NC}"
}

# 执行测试
run_all_tests 